package com.chen.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.common.AuthAccess;
import com.chen.springboot.common.Result;
import com.chen.springboot.entity.Item;
import com.chen.springboot.service.ItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Chen
 * @Date 2024/12/18 20:42
 */
@Api(tags = {"商品接口"})
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @ApiOperation("查询所有的接口")
    @GetMapping("/getAll")
    public Result<?> getAll(@RequestParam(defaultValue = "") String search) {
        List<Item> items = itemService.getAll(search);
        return Result.success(items);
    }

    @ApiOperation("新增商品")
    @PostMapping
    public Result<?> save(@RequestBody Item item) {
        itemService.save(item);
        return Result.success();
    }

    @ApiOperation("更新商品信息")
    @PutMapping
    public Result<?> update(@RequestBody Item item) {
        itemService.update(item);
        return Result.success();
    }

    @ApiOperation("删除商品信息")
    @DeleteMapping
    public Result<?> delete(@RequestParam Long id) {
        itemService.delete(id);
        return Result.success();
    }

    @ApiOperation("获取商品")
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page<Item> itemPage = itemService.findPage(pageNum, pageSize, search);
        return Result.success(itemPage);
    }

    @ApiOperation("根据id查找商品")
    @AuthAccess
    @GetMapping("/one")
    public Result<?> findById(@RequestParam Integer id) {
        Item item = itemService.findById(id);
        return Result.success(item);
    }

    @ApiOperation("根据 cid 来查询不同分类的商品")
    @AuthAccess
    @GetMapping("/category")
    public Result<?> findByCid(@RequestParam Integer cid) {
         List<Item> items=itemService.findByCid(cid);
        return Result.success(items);
    }
    
    @ApiOperation("根据热门系列筛选商品")
    @AuthAccess
    @GetMapping("/getByHotSeries")
    public Result<?> getByHotSeries(@RequestParam String hotSeries, @RequestParam(defaultValue = "") String search) {
        List<Item> items = itemService.getByHotSeries(hotSeries, search);
        return Result.success(items);
    }
}